package com.nd.controller.admin;


import com.nd.pojo.bean.Groupon;
import com.nd.pojo.bean.GrouponRules;
import com.nd.pojo.bo.GroupAddBo;
import com.nd.pojo.bo.GrouponData;
import com.nd.pojo.vo.BaseRespVo;
import com.nd.pojo.vo.GrouponCreateVO;
import com.nd.pojo.vo.GrouponListRecordVO;
import com.nd.pojo.vo.GrouponRulesVo;
import com.nd.service.GrouponService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class GrouponController {

    @Autowired
    GrouponService grouponService;

    @RequestMapping("groupon/list")
    public BaseRespVo grouponRules(GrouponData grouponData){
        GrouponRulesVo grouponRulesVo = grouponService.queryGrouponRulesVo(grouponData);
        return BaseRespVo.ok(grouponRulesVo);
    }

    @RequestMapping("groupon/delete")
    public BaseRespVo grouponDelete(@RequestBody GrouponRules rules){
        int delete = grouponService.deleteRules(rules);
        if(delete != 0) return BaseRespVo.fail();
        return BaseRespVo.ok();
    }

    @RequestMapping("groupon/create")
    public BaseRespVo grouponCreate(@RequestBody GroupAddBo rules){
//        if(rules.getGoodsId()==null ||
//            rules.getDiscount()==null||
//            rules.getDiscountMember()==null||
//            rules.getExpireTime()==null) return BaseRespVo.fail("参数不能为空！");
        GrouponCreateVO addRules = grouponService.addRules(rules);
        if(addRules == null) BaseRespVo.fail("商品不存在，确认id！");
        if (addRules.getGoodsId() != rules.getGoodsId()) BaseRespVo.fail("商品id不存在");
        return BaseRespVo.ok(addRules);
    }

    @RequestMapping("groupon/update")
    public BaseRespVo grouponUpdate(@RequestBody GrouponRules grouponRules) {
        grouponService.grouponUpdate(grouponRules);
        return BaseRespVo.ok();
    }

    @RequestMapping("groupon/listRecord")
    public BaseRespVo grouponListRecord(Integer page, Integer limit, String sort, String order) {
        GrouponListRecordVO grouponListRecordVO = grouponService.grouponListRecord(page,limit,sort,order);
        return BaseRespVo.ok(grouponListRecordVO);
    }

}
